﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ATM_Pro.Module;
using System.Reflection;
using System.IO;
using System.Diagnostics;
using ATM_Pro.Public;

namespace ATM_Pro.Query
{
    public partial class TransInfoQuery : Form
    {
        public TransInfoQuery()
        {
            InitializeComponent();
        }

        private List<ATM> Atms { get; set; }

        private void InitCombox()
        {
            cmbCamera.Items.Clear();
            cmbAtmAddress.Items.Clear();

            Atms = Configuration.GetInstance().Atms;
            foreach (ATM atm in Atms)
            {
                cmbAtmAddress.Items.Add(Enum.GetName(typeof(AtmAddress), atm.AddId));
            }
        }

        //Control cmbAtmAddress SelectedIndexChanged event
        private void cmbAtmAddress_SelectedIndexChanged(object sender, EventArgs e)
        {
            cmbCamera.Items.Clear();
            int index = (int)Enum.Parse(typeof(AtmAddress), cmbAtmAddress.Text);
            foreach (ATM atm in Atms)
            {
                if (atm.AddId == index)
                {
                    foreach (Camera cam in atm.Cams)
                    {
                        cmbCamera.Items.Add(cam.Name);
                    }
                }
            }
        }

        //Control btnQuery Click event
        private void btnQuery_Click(object sender, EventArgs e)
        {
            BindData(0, 30);
        }

        //Control btnCancel Click event
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
        }

        private void BindData(int index, int pagecount)
        {

            QeuryInfoHandle query = new QeuryInfoHandle();
            DateTime bdate = dtpBeginTime.Value;
            DateTime btime = tedBeginTime.Time;

            DateTime edate = dtpEndTime.Value;
            DateTime etime = tedEndTime.Time;

            DateTime begintime = new DateTime(bdate.Year, bdate.Month, bdate.Day, btime.Hour, btime.Minute, btime.Second);
            DateTime endtime = new DateTime(edate.Year, edate.Month, edate.Day, etime.Hour, etime.Minute, etime.Second);

            string atmName = cmbAtmAddress.Text;
            string camName = cmbCamera.Text;
            string msg = "";

            if (QeuryInfoAnalyze.CheckQueryParams(begintime, endtime, atmName, camName, "", false, ref msg))
            {
                int count = 0;
                DataSet resWarnInfo = query.Query(begintime, endtime, atmName, camName, index, pagecount, ref count);

                if (resWarnInfo != null && resWarnInfo.Tables.Count > 0 && resWarnInfo.Tables[0].Rows.Count > 0)
                {
                    ShowDataInToolBar(count, pagecount, index);
                    dgvResult.DataSource = resWarnInfo.Tables[0];
                }
                else
                {
                    MessageBox.Show("没有找到相关信息！");
                    dgvResult.DataSource = resWarnInfo;
                }
            }
            else
            {
                MessageBox.Show(msg, "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        int countpage = 0;

        private void ShowDataInToolBar(int count, int pagecount, int index)
        {
            countpage = (count % pagecount > 0) ? (count / pagecount) + 1 : (count / pagecount);
            if (countpage >= 2) tsbBeginSearch.Enabled = true;
            tspHide.Text = index.ToString();
            tspAll.Text = string.Format("第 {0}/{1} 页", index + 1, countpage);
        }

        private void tspFirst_Click(object sender, EventArgs e)
        {
            if (Convert.ToInt32(tspHide.Text.Trim()) == 0)
                return;
            BindData(0, 30);
        }

        private void tspNext_Click(object sender, EventArgs e)
        {
            int dd = Convert.ToInt32(tspHide.Text.Trim()) + 1;
            if (dd >= countpage) return;
            BindData(dd, 30);
        }

        private void txtPre_Click(object sender, EventArgs e)
        {
            int dd = Convert.ToInt32(tspHide.Text.Trim()) - 1;
            if (dd < 0) return;
            BindData(dd, 30);
        }

        private void tspEnd_Click(object sender, EventArgs e)
        {
            if (Convert.ToInt32(tspHide.Text.Trim()) == countpage - 1) return;
            BindData(countpage - 1, 30);
        }

        private void tsbBeginSearch_Click(object sender, EventArgs e)
        {
            try
            {
                int val = Convert.ToInt32(txtJump.Text.ToString());
                if (!(val > 0 && val <= countpage))
                {
                    MessageBox.Show("输入跳转页数为1-{0}的整数！", countpage.ToString());
                    return;
                }
                BindData(val - 1, 30);
            }
            catch (FormatException ex)
            {
                MessageBox.Show("请输入正整数！");
                return;
            }
        }


        private void dgvResult_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvResult.Focused)
            {
                DataGridViewRow dr = dgvResult.CurrentRow;
                if (dr == null) return;
                int curIndex = dr.Cells[0].RowIndex;
                //MessageBox.Show(curIndex.ToString());
                string path = dr.Cells["cBigPicPath"].Value.ToString();
                string temp = ImageHandle.AnalyzLiveImgFileName(path);
                if (temp != "")
                {
                    if (File.Exists(temp))
                    {
                        Image img = Image.FromFile(temp);
                        if (picWarnInfo.Image != null)
                        {
                            picWarnInfo.Image.Dispose();
                            picWarnInfo.Image = null;
                        }
                        picWarnInfo.Image = img;
                    }

                    QeuryInfoHandle queryInfo = new QeuryInfoHandle();
                    DataTable dt = queryInfo.GetTransInfoByPath(path);
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        DataRow row = dt.Rows[0];
                        lblTransCardID.Text = string.Format("交易卡号 {0}", row["tCardId"].ToString());
                        lblTransTime.Text = string.Format("交易时间 {0}", row["tTransTime"].ToString());
                        lblTransAddress.Text = string.Format("交易地点 {0}", row["Atm_Address"].ToString());
                        lblCameraId.Text = string.Format("照相机位置 {0}", row["Cam_Name"].ToString());
                    }
                }
            }
        }

        private void dgvResult_DoubleClick(object sender, EventArgs e)
        {
            if (dgvResult.Focused == true && dgvResult.CurrentRow != null)
            {
                DataGridViewRow dr = dgvResult.CurrentRow;
                int curIndex = dr.Index;
                if (curIndex > 0)
                {
                    VideoHandle vhandle = new VideoHandle();
                    string path = dr.Cells["cBigPicPath"].Value.ToString();
                    path = GeneratorVideoPath(path);
                    if (vhandle.HasVideo(path))
                        Process.Start(path);
                    else
                        MessageBox.Show("没有找到相关视频！");
                }
            }
        }

        private string GeneratorVideoPath(string path)
        {
            //D:\ImageOutput\00\00\RealtimeMessage\20091014\16\02\20091014160240-0002.jpg
            string fileName = Path.GetFileName(path);
            path = path.Replace(@"\" + fileName, ".avi");
            return path.Replace(PacketType.TradeMessage + "\\" + Properties.Settings.Default.BigImg, PacketType.RealtimeMessage.ToString());
        }

        private void TransInfoQuery_Load(object sender, EventArgs e)
        {
            InitCombox();
            tsbBeginSearch.Enabled = false;
        }

    }
}
